package com.hk.Executor;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.hk.config.MyConfiguration;
import com.hk.configBean.MyMappedStatement;
import com.hk.parse.SqlParse;
import com.hk.utils.ReflectUtil;


/**
 * 有MyConfiguration对象 与数据库连接   得到  Connection 对象
 * configuration可以得到mappedstatement 对象，可以通过它得到    id returnType sql
 * @author le-px
 *
 */
public class MyExecutor implements Executor{

	MyConfiguration myConfiguration;
	public MyExecutor(MyConfiguration myConfiguration) {
		this.myConfiguration = myConfiguration;
	}
	@Override
	public  List<Object> query(MyMappedStatement mapped, Object[] parameter) {
		List<Object> list = new ArrayList<>();
		Connection conn = getConncet();
		String sql = mapped.getSql();
		//调用sqlParse 进行转化sql；
		sql = SqlParse.parse(sql);
		Object resultType = mapped.getResultType();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			if(parameter[0] instanceof Integer) {
				ps.setInt(1,(Integer)parameter[0] );
			}else if(parameter[0]  instanceof Double) {
				ps.setDouble(1,(Double)parameter[0] );
			}else if(parameter[0]  instanceof Float) {
				ps.setFloat(1,(Float)parameter[0] );
			}else if(parameter[0]  instanceof String) {
				ps.setString(1,(String)parameter[0] );
			}else if(parameter[0]  instanceof Long) {
				ps.setLong(1,(Long)parameter[0] );
			}else if(parameter[0]  instanceof Date) {
				ps.setDate(1,(Date)parameter[0] );
			}
			rs = ps.executeQuery();
			ResultSetHandler(rs,list,resultType);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
				try {
					if(rs!=null)
					rs.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				try {
					if(ps!=null)
					ps.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			try {
				if(conn!=null)
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		//得到了ResultSet
		return list;
	}
	
	/**遍历结果集 */
	public  void ResultSetHandler(ResultSet resultSet,List<Object> list,Object resultType) {
		Object obj = new Object();
		try {
				while(resultSet.next()){
					//调用反射得到bean
					obj = getNewInstance(resultType);
					//通过反射，将结果集的值注入到bean中
					list.add(ReflectUtil.setPropToBeanFromResultSet(obj,resultSet));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
	public Connection getConncet() {
		Connection conn = myConfiguration.getConn();
		return conn;
	}
	
	/** 返回值是个List 需要多个Bean 通过反射得到更多的Bean */
	@SuppressWarnings("unused")
	private Object getNewInstance(Object resultType) {
		Object newInstance = null;
		try {
			newInstance = resultType.getClass().newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return newInstance;
	}
	
}
